<?php
/**
 * Created by PhpStorm
 * User: Jhinwy
 * Date: 5/20/24
 * Time: 4:52 PM
 */

namespace App\Http\Controllers\Admin;


use App\Services\CorpService;
use Illuminate\Http\Request;
use App\Http\Controllers\ResourceController;

class CorpController extends ResourceController
{
    protected $service;

    public function __construct(CorpService $service)
    {
        $this->service = $service;
    }


    /**
     * 获取授权企业列表
     *
     * @param Request $request
     * @return mixed
     */
    public function getCorpList(Request $request)
    {
        return $this->responseWithSuccess($this->service->getCorpList($request->post()));
    }


    /**
     * 获取所有企业信息
     *
     * @return mixed
     */
    public function getAll()
    {
        return $this->responseWithSuccess($this->service->getALl());
    }

    /**
     * 事件回调
     *
     * @param Request $request
     * @return mixed
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function eventCallback(Request $request)
    {
        $msgSignature = $request->get('msg_signature');
        $timestamp    = $request->get('timestamp');
        $nonce        = $request->get('nonce');
        $echostr      = $request->get('echostr');
        $contents     = $request->getContent();

        $errCode = $this->service->eventCallback($msgSignature, $timestamp, $nonce, $echostr, $contents, $returnStr);

        if ($contents) {

            return 'success';
        }


        return $errCode == 0 ? $returnStr : $this->responseWithError("ERR: " . $errCode);
    }
}
